<?php
namespace App\Models;

class Follow extends BaseModel
{
    protected $table = "concern";

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }

    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 我的關注列表
     */
    public function getMyFollowList($request){
        $uid= $request['mid'];
        $reslute = $this->getOne(array('user_id'=>$uid));
        $d_id_arr = json_decode($reslute['doctor_ids']);
        $data = [];
        foreach($d_id_arr as $v){
             $doc = new Doctor();
              $d_info = $doc->getOne(array('id'=>$v));
             $data[] = $d_info;
        }
        $data['data'] = $data;
        if($data){
            $data['pageNoList'] = 1;
        }
        return $data;
    }

    /**
     * 添加關注
     */
    public function addFollow($request){
         $id = $request['d_id'];
         $user_id = $request['mid'];
         if(!$id){
              return ['state'=>0,'msg'=>'關注人信息錯誤'];
         }
         $reslute = $this->getOne(array('user_id'=>$user_id));
         $d_id_arr = [];
         if(!$reslute){
              array_unshift($d_id_arr,$id);
              $data['user_id'] =  $user_id;
              $data['doctor_ids'] = json_encode($d_id_arr);
              $res = $this->add($data);
         }else{
             $d_id_arr = json_decode($reslute['doctor_ids']);
             if(!empty($d_id_arr)&&in_array($id,$d_id_arr)){
                  return ['state'=>0,'msg'=>'你已經關注了該醫生'];
             }
             array_unshift($d_id_arr,$id);
             $data['doctor_ids'] = json_encode($d_id_arr);
             $where['user_id'] = $user_id;
             $res = $this->up($where,$data);
         }
         if(!$res){
             return ['state'=>0,'msg'=>'關注失敗'];
         }
         return ['state'=>1,'msg'=>'關注成功'];
    }

    /**
     * 取消關注
     */
    public function getCancleFollow($request){
        $uid= $request['mid'];
        $reslute = $this->getOne(array('user_id'=>$uid));
        $d_id_arr = [];
        $d_id_arr = json_decode($reslute['doctor_ids']);
        foreach($d_id_arr as $k => $v){
              if($v==$request['id']){
                  array_splice($d_id_arr,$k,1);
              }
        }
        $data['doctor_ids'] = json_encode($d_id_arr);
        $where['user_id'] = $uid;
        $res = $this->up($where,$data);
        if(!$res){
            return ['state'=>0,'msg'=>'取消失敗'];
        }
        return ['state'=>1,'msg'=>'取消成功'];
    }
}